package com.yantu.controller.leader;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yantu.bean.*;
import com.yantu.mapper.*;
import com.yantu.util.JWTUtil;
import com.yantu.util.Result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 综合实训课程设计
 * @since 2022-08-07
 */
@RestController
@RequestMapping("/leader")
@ApiModel("管理端用户接口")
public class LeaderUserController extends leaderUserLogController{

    @Autowired
    UserMapper userMapper;

    @Autowired
    ClassMapper classMapper;

    @Autowired
    JWTUtil jwtUtil;

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ExamPaperAnswerMapper examPaperAnswerMapper;

    @Autowired
    ExamPaperQuestionCustomerAnswerMapper examPaperQuestionCustomerAnswerMapper;

    @Autowired
    ExamPaperMapper examPaperMapper;


    //用户编辑
    @PutMapping("/user1")
    @ApiOperation("编辑用户信息")
    public Result editUserMessage(@RequestBody User user) {

        UpdateWrapper updateQuery = new UpdateWrapper<>();
        updateQuery.eq("id",user.getId());
        if(user.getUserName()!=null){
            updateQuery.set("user_name",user.getUserName());}
        if(user.getPassword()!=null){
            updateQuery.set("password",user.getPassword());}
        if(user.getRealName()!=null){
            updateQuery.set("real_name",user.getRealName());}
        if(user.getRole()!=null){
            updateQuery.set("role",user.getRole());}
        if(user.getStatus()!=null){
            updateQuery.set("status",user.getStatus());}
        if(user.getAge()!=null){
            updateQuery.set("age",user.getAge());}
        if(user.getSex()!=null){
            updateQuery.set("sex",user.getSex());}
        if(user.getBirthDay()!=null){
            updateQuery.set("birth_day",user.getBirthDay());}
        if(user.getPhone()!=null){
            updateQuery.set("phone",user.getPhone());}
        if(user.getUserLevel()!=null){
            updateQuery.set("user_level",user.getUserLevel());}
        int result  = userMapper.update(null,updateQuery);
        if(result>0){
            User user1 = userMapper.selectById(user.getId());
            String msg = "admin 成功编辑个人用户"+user.getUserName()+"信息";
            super.leaderAddLog(user,msg);
            System.out.println(user1);
            return Result.succ(200,"编辑成功",user1);
        }else {
            String msg = "admin 个人用户"+user.getUserName()+"信息编辑失败";
            super.leaderAddLog(user,msg);
            return Result.fail("编辑失败");
        }
    }

    //获取当前个人用户信息
    @GetMapping("/user")
    @ApiOperation("获取当前用户个人信息")
    public Result getUserInfo(HttpServletRequest request){
        //获取请求头token
        String token = request.getHeader("Authorization");
        //根据
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("token",token);
        User user = userMapper.selectOne(query);
        if (user==null){
            return Result.fail("获取失败",null);
        }
        //把查询到的信息放在需要的视图里面并返回给前端
        Map<String,Object> map = new HashMap<>();
        map.put("id",user.getId());
        map.put("userName",user.getUserName());
        map.put("realName",user.getRealName());
        map.put("age",user.getAge());
        map.put("role",user.getRole());
        map.put("sex",user.getSex());
        map.put("birthDay",user.getBirthDay());
        map.put("phone",user.getPhone());
        map.put("createTime",user.getCreateTime());
        map.put("lastActiveTime",user.getLogintime());
        map.put("status",user.getStatus());
        map.put("userLevel",user.getUserLevel());
        map.put("imagePath",user.getImagePath());

        return Result.succ(200,"获取成功",map);
    }

    //个人信息修改
    @PutMapping("/user2")
    @ApiOperation("个人信息修改")
    public Result editMyInfo(HttpServletRequest request,@ModelAttribute User user){
        //获取请求头token
        String token = request.getHeader("Authorization");
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("token",token);
        User user1 = userMapper.selectOne(query);
        if (user1==null){
            return Result.fail("获取失败",null);
        }
        UpdateWrapper updateQuery=new UpdateWrapper();
        updateQuery.eq("id",user1.getId());
        if(user.getUserName()!=null){
            updateQuery.set("user_name",user.getUserName());}
        if(user.getRealName()!=null){
            updateQuery.set("real_name",user.getRealName());}
        if(user.getRole()!=null){
            updateQuery.set("role",user.getRole());}
        if(user.getStatus()!=null){
            updateQuery.set("status",user.getStatus());}
        if(user.getAge()!=null){
            updateQuery.set("age",user.getAge());}
        if(user.getSex()!=null){
            updateQuery.set("sex",user.getSex());}
        if(user.getBirthDay()!=null){
            updateQuery.set("birth_day",user.getBirthDay());}
        if(user.getPhone()!=null){
            updateQuery.set("phone",user.getPhone());}
        if(user.getUserLevel()!=null){
            updateQuery.set("user_level",user.getUserLevel());}
        if(user.getImagePath()!=null){
            updateQuery.set("image_path",user.getImagePath());}
        int result  = userMapper.update(null,updateQuery);
        if(result>0){

            User user2 = userMapper.selectById(user.getId());
            String msg = "用户"+user.getUserName()+"信息编辑成功";
            super.leaderAddLog(user,msg);
            return Result.succ(200,"编辑成功",user2);
        }else {
            User user2 = userMapper.selectById(user.getId());
            String msg = "用户"+user.getUserName()+"信息编辑失败";
            super.leaderAddLog(user,msg);
            return Result.fail("编辑失败");
        }
    }

    @PostMapping("/Register")
    @ApiOperation("注册小队长用户")
    public Result registorLeader(@RequestBody User user){
        int result  = userMapper.insert(user);
        if(result>0){
            String msg = user.getUserName()+"注册成功";
            super.leaderAddLog(user,msg);
            return Result.succ("注册成功",null);
        }else {
           return Result.fail("注册失败");
        }
    }
}
